# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
中移动项目的接口,提供对外
# 文件：api_msydloan
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from datetime import datetime

import requests

from interfacetest.baseset.msydloan_api import API_MSYD
from msydwork.config.config_msydloan import w_url
from pycacho.cachobase.base_model import encode_base64
from pycacho.cachobase.logger import Logger

logger = Logger("api_msydloan").get_log()


headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json',
        }


def apply(name,mobile,id_card,hb_no,idCardFrontID,idCardBackID,usrProvNo):
    fu_url = w_url + '/cmpay/credit/v1/apply'
    qryCreditId = '002647'+ datetime.now().strftime('%Y%m%d') + mobile
    data = API_MSYD['apply']
    data['qryCreditId'] = qryCreditId
    data['hbUsrNo'] = hb_no
    data['usrIdCard'] = id_card
    data['usrIdName'] = name
    data['bankCardName'] = name
    data['mblNo'] = mobile
    data['idCardFrontID'] = idCardFrontID
    data['idCardBackID'] = idCardBackID
    data['usrProvNo'] = usrProvNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信申请接口响应信息：%s", data)


def apply_status(qryCreditId,hbUsrNo):
    fu_url = w_url + '/cmpay/credit/v1/apply/status'
    data = API_MSYD['apply_status']
    data['qryCreditId'] = qryCreditId
    data['hbUsrNo'] = hbUsrNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信状态查询接口响应信息：%s", data)


def actualOrgPreQuery(hbUsrNo,brwOrdNo):
    fu_url = w_url + '/cmpay/quota/v1/actualOrgPreQuery'
    data = API_MSYD['actualOrgPreQuery']
    data['hbUsrNo'] = hbUsrNo
    data['brwOrdNo'] = brwOrdNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("借款实际出资方预查询接口响应信息：%s", data)


def sign(hbUsrNo,brwOrdNo):
    fu_url = w_url + '/cmpay/contract/v1/sign'
    data = API_MSYD['sign']
    data['hbUsrNo'] = hbUsrNo
    data['brwOrdNo'] = brwOrdNo
    data['base64Content'] = encode_base64('E:\zenglingwei\\test\\cpchZhongMaLoan.pdf')
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("合同签章接口接口响应信息：%s", data)


def freeze(hbUsrNo,brwOrdNo,mblNo,usrIdCard,depProvNo,loanMonth,provStgDay):
    fu_url = w_url + '/cmpay/quota/v1/freeze'
    data = API_MSYD['freeze']
    data['hbUsrNo'] = hbUsrNo
    data['brwOrdNo'] = brwOrdNo
    data['mblNo'] = mblNo
    data['actBrwMblNo'] = mblNo
    data['actBrwIdNo'] = usrIdCard
    data['depProvNo'] = depProvNo
    data['loanMonth'] = loanMonth
    data['provStgDay'] = provStgDay
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("借款申请接口响应信息：%s", data)


def freeze_status(brwOrdNo,orgOrdNo):
    fu_url = w_url + '/cmpay/quota/v1/freeze/status'
    data = API_MSYD['freeze_status']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("借款查询接口响应信息：%s", data)

# 提货人脸核身
def pwCompare():
    fu_url = w_url + '/cmpay/goods/v1/pwCompare'
    data = API_MSYD['pwCompare']
    data['brwOrdNo'] = brwOrdNo
    data['hbUsrNo'] = hbUsrNo
    data['base64Image'] = encode_base64('E:\zenglingwei\\test\\201910171229372189.jpg')
    print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("套餐办理通知前的提货人脸核身接口响应信息：%s", data)

def block(brwOrdNo,orgOrdNo,mblNo,brwOrdDt,acpTm):
    fu_url = w_url + '/cmpay/quota/v1/block'
    data = API_MSYD['block']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['mblNo'] = mblNo
    data['brwOrdDt'] = brwOrdDt
    data['acpTm'] = acpTm
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("套餐办理通知接口接口响应信息：%s", data)

def queryRepaymentPlan(brwOrdNo,orgOrdNo):
    fu_url = w_url + '/cmpay/loan/v1/queryRepaymentPlan'
    data = API_MSYD['queryRepaymentPlan']
    data['mplOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("还款计划查询接口响应信息：%s", data)

# ms_loan有记录就可以
def preview(brwOrdNo,orgOrdNo):
    fu_url = w_url + '/cmpay/loan/v1/preview'
    data = API_MSYD['preview']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['rpyMod'] = '3'            # 0：还全部(购机不传),1-还某期,2-提前清贷,3-退货
    data['rpySeq'] = ''
    #print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("主动还款申请接口响应信息：%s", data)

# ms_loan有记录就可以
def repayment(num,pay_date,brwOrdNo,orgOrdNo):
    fu_url = w_url + '/cmpay/loan/v1/repayment'
    data = API_MSYD['repayment']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['rpyOrdNo'] = 'r' + datetime.now().strftime('%Y%m%d%H%M%S')
    data['rpyMod'] = '3'            # 0：还全部(购机不传),1-还某期,2-提前清贷,3-退货
    data['rpySeq'] = str(num)             # 1-还某期的期数
    data['rpyAmt'] = '948.75'        # 51.25+           958.33+28.75  1000+38.33   41.67+8.33=50  1046.66
    data['rpyOrdDt'] = pay_date
    #print(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("扣款结果通知接口响应信息：%s", data)


def updateLoan():
    fu_url = w_url + '/test/updateLoan'
    req1 = requests.post(fu_url, headers=headers)
    data = req1.text
    logger.info("处理数据接口响应信息：%s", data)


if __name__ == '__main__':
    hbUsrNo = '99430321198702061217'
    brwOrdNo = '560202011240000002'
    mblNo = '18773228215'
    usrIdCard = '430321198702061217'
    qryCreditId = '026431022197712104221'
    orgOrdNo = 'LW20201124HB000012'
    brwOrdDt = '20200421'
    acpTm = '20200421151512'
    #freeze(hbUsrNo, brwOrdNo, mblNo, usrIdCard,'18','24','20')
    #freeze_status(brwOrdNo, orgOrdNo)
    #actualOrgPreQuery(hbUsrNo,brwOrdNo)
    #apply_status(qryCreditId, hbUsrNo)
    #block(brwOrdNo, orgOrdNo, mblNo, brwOrdDt, acpTm)
    #queryRepaymentPlan(brwOrdNo, orgOrdNo)
    #preview(brwOrdNo, orgOrdNo)
    repayment('', '20201124', brwOrdNo, orgOrdNo)
    #freeze_status(brwOrdNo,orgOrdNo)
    #updateLoan()